﻿using System;
using System.Collections.Generic;
using AutoMapper;
using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;

namespace Pay.Service.AutoMapper {
	public class MerchantRegisterMapper : IStartupDto {
		private IMapperConfigurationExpression _cfg;
		public MerchantRegisterMapper(IMapperConfigurationExpression cfg) {
			_cfg = cfg;
		}
		public void Execute() {
			Create();
		}
		protected virtual void Create() {
			_cfg.CreateMap<MerchantRegister, merchant_register_dto>()
				.ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.MerchantId))
				.ForMember(dest => dest.pay_channel, src => src.MapFrom(c => c.PayChannel))
				.ForMember(dest => dest.settle_type, src => src.MapFrom(c => c.SettleType))
				.ForMember(dest => dest.merchant_no, src => src.MapFrom(c => c.MerchantNo))
				.ForMember(dest => dest.bank_code, src => src.MapFrom(c => c.BankCode))
				.ForMember(dest => dest.bank_name, src => src.MapFrom(c => c.BankName))
				.ForMember(dest => dest.branch_code, src => src.MapFrom(c => c.BranchCode))
				.ForMember(dest => dest.branch_name, src => src.MapFrom(c => c.BranchName))
				.ForMember(dest => dest.account_no, src => src.MapFrom(c => c.AccountNo))
				.ForMember(dest => dest.account_name, src => src.MapFrom(c => c.AccountName))
				.ForMember(dest => dest.busi_type, src => src.MapFrom(c => c.BusiType))
				.ForMember(dest => dest.id_card, src => src.MapFrom(c => c.IdCard))
				.ForMember(dest => dest.open_id, src => src.MapFrom(c => c.OpenId))
				.ForMember(dest => dest.region_id, src => src.MapFrom(c => c.RegionId))
				.ForMember(dest => dest.is_success, src => src.MapFrom(c => c.IsSuccess))
				.ForMember(dest => dest.card_handheld, src => src.MapFrom(c => c.CardHandheld))
				.ForMember(dest => dest.cards, src => src.MapFrom(c => !string.IsNullOrEmpty(c.Cards) ? c.Cards.ToObject<List<card_status>>() : new List<card_status>()));
			_cfg.CreateMap<merchant_register_dto, MerchantRegister>()
				.ForMember(dest => dest.MerchantId, src => src.MapFrom(c => c.merchant_id))
				.ForMember(dest => dest.PayChannel, src => src.MapFrom(c => c.pay_channel))
				.ForMember(dest => dest.SettleType, src => src.MapFrom(c => c.settle_type))
				.ForMember(dest => dest.MerchantNo, src => src.MapFrom(c => c.merchant_no))
				.ForMember(dest => dest.BankCode, src => src.MapFrom(c => c.bank_code))
				.ForMember(dest => dest.BankName, src => src.MapFrom(c => c.bank_name))
				.ForMember(dest => dest.BranchCode, src => src.MapFrom(c => c.branch_code))
				.ForMember(dest => dest.BranchName, src => src.MapFrom(c => c.branch_name))
				.ForMember(dest => dest.AccountNo, src => src.MapFrom(c => c.account_no))
				.ForMember(dest => dest.AccountName, src => src.MapFrom(c => c.account_name))
				.ForMember(dest => dest.BusiType, src => src.MapFrom(c => c.busi_type))
				.ForMember(dest => dest.IdCard, src => src.MapFrom(c => c.id_card))
				.ForMember(dest => dest.OpenId, src => src.MapFrom(c => c.open_id))
				.ForMember(dest => dest.RegionId, src => src.MapFrom(c => c.region_id))
				.ForMember(dest => dest.CardHandheld, src => src.MapFrom(c => c.card_handheld))
				.ForMember(dest => dest.IsSuccess, src => src.MapFrom(c => c.is_success))
				.ForMember(dest => dest.Cards, src => src.MapFrom(c => c.cards.ToJson()));

			_cfg.CreateMap<merchant_register_model, MerchantRegister>()
				.ForMember(dest => dest.PayChannel, src => src.MapFrom(c => c.pay_channel))
				.ForMember(dest => dest.SettleType, src => src.MapFrom(c => c.settle_type))
				.ForMember(dest => dest.MerchantNo, src => src.MapFrom(c => c.reg_merchant_no))
				.ForMember(dest => dest.BankCode, src => src.MapFrom(c => c.bank_code))
				.ForMember(dest => dest.BankName, src => src.MapFrom(c => c.bank_name))
				.ForMember(dest => dest.BranchCode, src => src.MapFrom(c => c.branch_code))
				.ForMember(dest => dest.BranchName, src => src.MapFrom(c => c.branch_name))
				.ForMember(dest => dest.AccountNo, src => src.MapFrom(c => c.account_no))
				.ForMember(dest => dest.AccountName, src => src.MapFrom(c => c.account_name.Trim()))
				.ForMember(dest => dest.BusiType, src => src.MapFrom(c => c.busi_type))
				.ForMember(dest => dest.IdCard, src => src.MapFrom(c => c.id_card.Trim()))
				.ForMember(dest => dest.OpenId, src => src.MapFrom(c => c.open_id))
				.ForMember(dest => dest.RegionId, src => src.MapFrom(c => c.region_id))
				.ForMember(dest => dest.CardHandheld, src => src.MapFrom(c => c.card_handheld));

			_cfg.CreateMap<auth_info_setting, merchant_register_model>();
			_cfg.CreateMap<merchant_register_dto, merchant_register_model>()
				.ForMember(dest => dest.merchant_no, src => src.Ignore())
				.ForMember(dest => dest.reg_merchant_no, src => src.MapFrom(c => c.merchant_no));

			_cfg.CreateMap<auth_info_setting, MerchantRegister>()
				.ForMember(dest => dest.BankCode, src => src.MapFrom(c => c.bank_code))
				.ForMember(dest => dest.BankName, src => src.MapFrom(c => c.bank_name))
				.ForMember(dest => dest.BranchCode, src => src.MapFrom(c => c.branch_code))
				.ForMember(dest => dest.BranchName, src => src.MapFrom(c => c.branch_name))
				.ForMember(dest => dest.AccountNo, src => src.MapFrom(c => c.account_no))
				.ForMember(dest => dest.AccountName, src => src.MapFrom(c => c.account_name))
				.ForMember(dest => dest.BusiType, src => src.MapFrom(c => c.busi_type))
				.ForMember(dest => dest.IdCard, src => src.MapFrom(c => c.id_card))
				.ForMember(dest => dest.Mobile, src => src.MapFrom(c => c.mobile))
				.ForMember(dest => dest.RegionId, src => src.MapFrom(c => c.region_id))
				.ForMember(dest => dest.CardHandheld, src => src.MapFrom(c => c.card_handheld));
		}
	}
}
